<?php
namespace System\Core\LogHandlers;

use System\Core\Log;

abstract class BaseHandler {
    protected $level;
    
    public function __construct($level = Log::DEBUG) {
        $this->level = $level;
    }
    
    public function isHandling($level) {
        return $level >= $this->level;
    }
    
    abstract public function handle($level, $message, array $context = []);
    
    protected function formatMessage($level, $message, $context) {
        $levelName = Log::getLevelName($level);
        $timestamp = date('Y-m-d H:i:s');
        
        // 替换上下文中的占位符
        $replace = [];
        foreach ($context as $key => $val) {
            $replace['{' . $key . '}'] = $val;
        }
        
        $message = strtr($message, $replace);
        
        return "[{$timestamp}] {$levelName}: {$message}" . PHP_EOL;
    }
}